package cn.jg.framework.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author fanfeveryahoo@gmail.com
 * @date 2019/2/13
 */
@Slf4j
public class DatabaseUtils {

	private static final String LINE_FEEDS_STR = "\n";
	private static final String CREATE_TABLE_STR = "CREATE TABLE";
	private static final String DOUBLE_HYPHEN_STR = "-- ";
	private static final String INDEX_STR = " INDEX";
	private static final String UNIQUE_KEY_STR = "UNIQUE KEY";
	private static final String KEY_STR = "KEY";
	private static final String ON_STR = " ON ";
	private static final String BACK_TICK_STR = "`";
	private static final String OPEN_BRAKET = "(";
	private static final String CLOSE_BRAKET = ")";
	private static final String COMMA = ",";
	private static final String CREATE_UNIQUE_INDEX_STR = "CREATE UNIQUE INDEX ";
	private static final String CREATE_INDEX_STR = "CREATE INDEX ";
	private static final String COMMENT_OUT_STR = "/*";
	private static final String SEMICOLON = ";";

	private DatabaseUtils() {

	}

	/**
	 * 提取出create index语句，不包括主键，适用于初始化，如需diff，则推荐liquibase的脚本
	 *
	 * @param filePath mysqldump -uroot -padmin --no-data --databases udesk >schema.sql文件路径
	 */
	public static void extractCreateIndexDDL(String filePath) {
		StringBuilder contentBuilder = new StringBuilder(500);
		String[] tableName = new String[1];
		try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
			stream.forEach(lineStr -> {
				lineStr = lineStr.trim();
				if (StringUtils.startsWith(lineStr, COMMENT_OUT_STR)) {

				} else if (StringUtils.startsWith(lineStr, CREATE_TABLE_STR)) {
					String currentTableName = StringUtils.substringBetween(lineStr, BACK_TICK_STR);
					tableName[0] = currentTableName;
					contentBuilder.append(LINE_FEEDS_STR).append(DOUBLE_HYPHEN_STR).append(currentTableName).append(INDEX_STR);
				} else if (StringUtils.startsWith(lineStr, UNIQUE_KEY_STR)) {
					String[] segmentArray = StringUtils.substringsBetween(lineStr, BACK_TICK_STR, BACK_TICK_STR);
					contentBuilder.append(LINE_FEEDS_STR).append(CREATE_UNIQUE_INDEX_STR).append(segmentArray[0]).append(ON_STR).append(tableName[0]).append(OPEN_BRAKET).append(Arrays.stream(segmentArray).skip(1).collect(Collectors.joining(COMMA))).append(CLOSE_BRAKET).append(SEMICOLON);
				} else if (StringUtils.startsWith(lineStr, KEY_STR)) {
					String[] segmentArray = StringUtils.substringsBetween(lineStr, BACK_TICK_STR, BACK_TICK_STR);
					contentBuilder.append(LINE_FEEDS_STR).append(CREATE_INDEX_STR).append(segmentArray[0]).append(ON_STR).append(tableName[0]).append(OPEN_BRAKET).append(Arrays.stream(segmentArray).skip(1).collect(Collectors.joining(COMMA))).append(CLOSE_BRAKET).append(SEMICOLON);
				}
			});
		} catch (IOException e) {
			log.error("读取文件失败", e);
		}
		log.info(contentBuilder.toString());
	}

}
